﻿using System.IO;
using System.Net;
using System.Windows.Forms;

namespace MyMovies
{
    internal class FtpManager
    {
        public static bool Download(string path)
        {
            //FtpWebRequest reqFTP;
            //reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://maurorogledi.myftp.org/Video/_Elenco%20Film/MyMovies.db"));
            //reqFTP.UseBinary = true;
            //reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
            //reqFTP.Proxy = null;
            //reqFTP.KeepAlive = false;
            //reqFTP.UsePassive = true;
            //FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

            WebClient request = new WebClient();
            if (Properties.Settings.Default.Username == string.Empty)
            {
                if (new credentialForm().ShowDialog() == DialogResult.Cancel || Properties.Settings.Default.Username == string.Empty)
                    return false;
            }
            request.Credentials = new NetworkCredential(Properties.Settings.Default.Username, Cryption.Decrypt(Properties.Settings.Default.Password));
            try
            {
                byte[] filedata = request.DownloadData(path);
                FileStream file = File.Create("MyMovies.db");
                file.Write(filedata, 0, filedata.Length);
                file.Close();
            }
            catch (WebException exc)
            {
                Properties.Settings.Default.Username = string.Empty;
                Properties.Settings.Default.Password = string.Empty;
                MessageBox.Show(exc.Message);
                return false;
            }

            return true;
        }
    }
}